//
//  AXMyCropManageVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/11/10.
//  Copyright © 2018 tzhg. All rights reserved.
//
//作物管理
#import "AXMyCropManageVC.h"
#import "UIView+SDAutoLayout.h"
#import "UIViewController+HBD.h"
#import "AXMyChildBearingPeriodVC.h"
#import "AXMyFertilizationManageVC.h"
#import "AXMyIrrigateManageVC.h"
#import "AXMyDiseaseManageVC.h"
#import "AXMyOtherManageVC.h"


@interface AXMyCropManageVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSDictionary *dataDict;
@property(nonatomic,strong)NSMutableArray *isOpenArr;
@property(nonatomic,strong)NSArray *sectionNameArr;
@property(nonatomic,strong)NSArray  *firstDataArr;
@property(nonatomic,strong)NSArray  *thirdArr;
@property(nonatomic,strong)NSArray  *secondArr;
@property (nonatomic, strong) NSMutableArray *stateAry;
@property (nonatomic , strong) UIView *headView;
@property (nonatomic , strong) UIImageView *img;

@end


@implementation AXMyCropManageVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupNavigationItems];
    self.view.backgroundColor=[UIColor whiteColor];
    
 
    self.firstDataArr=@[@"经度E100'-100'59",@"海拔1105-3299米",@"年均气温30",@"历史最低温度15℃",@"年霜期100-200天"];
    self.secondArr=@[@"作物名称:石榴",@"品种:突尼斯",@"种植时间:2018-10-10",@"种植地址：云南省昆明市盘龙区",@"种植方式:连作"];
    self.thirdArr=@[@"萌芽期",@"施肥管理",@"灌溉管理",@"病害及防治方法",@"虫害及防治方法",@"草害及防治方法",@"其他农事操作"];
    _sectionNameArr=@[@"地理气象信息",@"作物信息",@"作物管理"];
    
    
    _stateAry = [NSMutableArray arrayWithArray:@[@"close", @"close", @"close"]];
    
    self.isOpenArr=[[NSMutableArray alloc] init];
    
    
    for (int i=0; i<self.firstDataArr.count; i++) {
        
        NSString *state=@"close";
        [self.isOpenArr addObject:state];
    }
    [self loadTableView];
    
    for (int i=0; i<self.secondArr.count; i++) {
        
        NSString*  state=@"close";
        [self.isOpenArr addObject:state];
    }
    [self loadTableView];
    
    
    for (int i=0; i<self.thirdArr.count; i++) {
        
        NSString*  state=@"close";
        [self.isOpenArr addObject:state];
    }
    [self loadTableView];
    
    
    
}
-(void)loadTableView

{
    
    
    _headView = [[UIView alloc]init];
    
    self.img = [[UIImageView alloc]init];
    self.img.contentMode =UIViewContentModeScaleAspectFill;
    self.img .clipsToBounds=YES;//
    self.img.backgroundColor = [UIColor whiteColor];
    self.img.image = [UIImage imageNamed:@"wdzwImage"];
    [self.headView addSubview:self.img];
    
    self.img.sd_layout
    .leftSpaceToView(self.headView, 0)
    .topSpaceToView(self.headView, 0)
    .rightSpaceToView(self.headView, 0)
    .bottomSpaceToView(self.headView, 0);
    
    
   
    
    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, Hn, SCREEN_WIDTH, TableViewHeight) style:UITableViewStyleGrouped];
    self.tableView.sectionFooterHeight=0;//从第二个开始，每个section之间的距离
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.view addSubview:self.tableView];
    
    self.tableView.tableHeaderView = _headView;
    [_headView layoutIfNeeded];//刷新布局
    _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 300);//headView 的大小
    self.tableView.tableHeaderView = _headView;

}


#pragma mark - action



//cell 的选中方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
    }
    else if (indexPath.section == 1){
        
    }
    
    switch (indexPath.row) {
            
        case 0:{
            //生育期
            AXMyChildBearingPeriodVC *vc = [[AXMyChildBearingPeriodVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
            
        }
            
            break;
            //施肥管理
        case 1:{
            
             AXMyFertilizationManageVC *vc = [[AXMyFertilizationManageVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
           
            
        }
            
            break;
            
            //灌溉管理
        case 2:{
            
            AXMyIrrigateManageVC * vc = [[AXMyIrrigateManageVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
            
        }
            
            break;
            //病害管理
        case 3:{
            
            AXMyDiseaseManageVC * vc = [[AXMyDiseaseManageVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
          
            
        }
            
            break;
        case 4:{
            
        }
            
            break;
        case 5:{
            
        }
            
            break;
        case 6:{
            
              AXMyOtherManageVC * vc = [[AXMyOtherManageVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
          
            
        }
            
            break;
            
        default:
            break;
    }
    
    
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 3;
}

/** 一个分区有几行 */

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSString *str = _stateAry[section];
    if (section == 0) {
        
        if ([str isEqualToString:@"close"]) {
            return 0;
        } else {
            return 5;

        }
    }
    
    else if (section == 1) {
        
        if ([str isEqualToString:@"close"]) {
            return 0;
        } else {
            return 5;
            
        }
    }
    
    if ([str isEqualToString:@"close"]) {
        return 0;
    } else {
        return 7;
        
    }
    
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString* identifer=@"cell";
    UITableViewCell*  cell=[tableView dequeueReusableCellWithIdentifier:identifer];
    
    if (!cell) {
        cell=[[UITableViewCell alloc] init];
    }
    
    if (indexPath.section == 0) {
        

    
        UILabel *leftStateLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, (SCREEN_WIDTH-30)/2, 44)];
        leftStateLabel.font = [UIFont systemFontOfSize:14];
        leftStateLabel.text=self.firstDataArr[indexPath.row];
        leftStateLabel.font = [UIFont systemFontOfSize:14];
        leftStateLabel.textColor = [UIColor lightGrayColor];
        [ cell.contentView addSubview:leftStateLabel];
        
        if (indexPath.row <= 1) {
            
          UILabel*  stateLabel=[[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-30)/2, 0, (SCREEN_WIDTH-30)/2, 44)];
            stateLabel.font = [UIFont systemFontOfSize:14];
            stateLabel.text=@[@"纬度E100'-100'59",@"海拔1105-3299米",@"年均气温30",@"历史最低温度15℃",@"年霜期100-200天"][indexPath.row];
            stateLabel.font = [UIFont systemFontOfSize:14];
            stateLabel.textColor = [UIColor lightGrayColor];
            [cell.contentView addSubview:stateLabel];
        }

       
    }
    
    else if (indexPath.section == 1) {
        
     
        
        UILabel*  stateLabel=[[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-30)/2, 0, (SCREEN_WIDTH-30)/2, 44)];
        
        if (indexPath.row == 0) {
            
            UILabel *leftStateLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, (SCREEN_WIDTH-30)/2, 44)];
            leftStateLabel.font = [UIFont systemFontOfSize:14];
            leftStateLabel.text=self.secondArr[indexPath.row];
            leftStateLabel.font = [UIFont systemFontOfSize:14];
            leftStateLabel.textColor = [UIColor lightGrayColor];
            [cell.contentView addSubview:leftStateLabel];
            stateLabel.text = @"作物目标:有机食品";
            
        }
        
        else if (indexPath.row == 1) {
            
            UILabel *leftStateLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, (SCREEN_WIDTH-30)/2, 44)];
            leftStateLabel.font = [UIFont systemFontOfSize:14];
            leftStateLabel.text=self.secondArr[indexPath.row];
            leftStateLabel.font = [UIFont systemFontOfSize:14];
            leftStateLabel.textColor = [UIColor lightGrayColor];
            [cell.contentView addSubview:leftStateLabel];
            stateLabel.text = @"种植面积:23亩";
            
        }
        
        UILabel *leftStateLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, (SCREEN_WIDTH-20), 44)];
        leftStateLabel.font = [UIFont systemFontOfSize:14];
        leftStateLabel.text=self.secondArr[indexPath.row];
        leftStateLabel.font = [UIFont systemFontOfSize:14];
        leftStateLabel.textColor = [UIColor lightGrayColor];
        [cell.contentView addSubview:leftStateLabel];
        stateLabel.font = [UIFont systemFontOfSize:14];
        stateLabel.font = [UIFont systemFontOfSize:14];
        stateLabel.textColor = [UIColor lightGrayColor];
        [cell.contentView addSubview:stateLabel];
        
    }
    
    else if (indexPath.section == 2) {
        
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        UILabel *leftStateLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, (SCREEN_WIDTH-30)/2, 44)];
        leftStateLabel.font = [UIFont systemFontOfSize:14];
        leftStateLabel.text=self.thirdArr[indexPath.row];
        leftStateLabel.font = [UIFont systemFontOfSize:14];
        leftStateLabel.textColor = [UIColor lightGrayColor];
        [cell.contentView addSubview:leftStateLabel];
        
        
        UILabel*  stateLabel=[[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-30)/2, 0, (SCREEN_WIDTH-30)/2, 44)];
        stateLabel.font = [UIFont systemFontOfSize:14];
        stateLabel.text= @"展开";
        stateLabel.textAlignment = NSTextAlignmentRight;
        stateLabel.font = [UIFont systemFontOfSize:14];
        stateLabel.textColor = [UIColor lightGrayColor];
        [cell.contentView addSubview:stateLabel];
        

        
    }
    
    return cell;
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 44;
    
}


-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView*  sectionBackView=[[UIView alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-40, 50)];
    sectionBackView.backgroundColor=[UIColor whiteColor];
    
    UIImageView*  lineImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
    [lineImageView setImage:[UIImage imageNamed:@"Google模糊灰.png"]];
    [sectionBackView addSubview:lineImageView];
    
    UILabel*  nameLabel=[[UILabel alloc] initWithFrame:CGRectMake(20, 0, (SCREEN_WIDTH-30)/2, 50)];
    nameLabel.text=[_sectionNameArr objectAtIndex:section];
    nameLabel.font = [UIFont systemFontOfSize:14];
    [sectionBackView addSubview:nameLabel];
    
    
    UIButton*  button=[UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    button.tag=100+section;
    [button addTarget:self action:@selector(ClickSection:) forControlEvents:UIControlEventTouchUpInside];
    [sectionBackView addSubview:button];
    
    
    UILabel*  numLabel=[[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-70, 0, 40, 50)];
    numLabel.text=@"收起";
    numLabel.font = [UIFont systemFontOfSize:14];
    numLabel.textColor = [UIColor lightGrayColor];
    [sectionBackView addSubview:numLabel];
    UIImageView* stateImage=[[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-30, 20, 15, 8)];
    if ([[_stateAry objectAtIndex:section] isEqualToString:@"open"]) {
        [stateImage setImage:[UIImage imageNamed:@"sectionOpen"]];
    }
    else if ([[_stateAry objectAtIndex:section] isEqualToString:@"close"]) {
        [stateImage setImage:[UIImage imageNamed:@"sectionClose"]];
    }
    [sectionBackView addSubview:stateImage];
    return sectionBackView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Action


-(void)ClickSection:(UIButton*)sender
{
//    sender.selected = !sender.selected;
    NSString*  state=[self.stateAry objectAtIndex:sender.tag-100];
    if ([state isEqualToString:@"open"]) {
        state=@"close";
    }else
    {
        state=@"open";
    }
    self.stateAry[sender.tag-100]=state;
    NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:sender.tag-100];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
    
    
}


- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    /** 搜索 */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"作物管理";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_FOURTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
    
}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}




@end
